<template>
  <el-container class="home-container">
    <el-header>
      <div>
        <img src="../assets/heima.png" alt="logo" />
        <span>电商后台管理系统</span>
      </div>
      <el-button type="warning" plain @click="logout">退出</el-button>
    </el-header>
    <el-container>
      <el-aside :width="isCollapse ? '64px' : '200px'">
        <el-menu
          background-color="#333744"
          text-color="#fff"
          active-text-color="#ffd04b"
          unique-opened
          router
          :collapse="isCollapse"
          :collapse-transition="false"
          :default-active="activePath"
        >
          <div class="toggle-button" @click="toggleMenu">|||</div>
          <el-submenu :index="item.id + ''" v-for="item in menuList" :key="item.id">
            <template slot="title">
              <i :class="iconsObj[item.id]"></i>
              <span>{{ item.authName }}</span>
            </template>
            <el-menu-item
              :index="'/' + subItem.path"
              v-for="subItem in item.children"
              :key="subItem.id"
              @click="activeHandle('/' + subItem.path)"
            >
              <i class="el-icon-menu"></i>
              <span>{{ subItem.authName }}</span>
            </el-menu-item>
          </el-submenu>
        </el-menu>
      </el-aside>
      <el-main>
        <router-view />
      </el-main>
    </el-container>
  </el-container>
</template>

<script>
export default {
  name: 'Home',
  data() {
    return {
      // 左侧菜单数据
      menuList: [],
      // 这里使用了一个奇淫技巧
      // 每次循环 id 都是唯一的，然后自己创建一个数据，
      // 键是每一个分类的 id, 值是 icon 的图标
      // 每次循环的时候，使用 iconsObj[id] 的方式获取到 icon 图标，从而展示图标
      iconsObj: {
        125: 'iconfont icon-user',
        103: 'iconfont icon-tijikongjian',
        101: 'iconfont icon-shangpin',
        102: 'iconfont icon-danju',
        145: 'iconfont icon-baobiao'
      },
      // 控制分类是否展开
      isCollapse: false,
      // 当前激活的分类
      activePath: ''
    }
  },
  created() {
    this.getMenus()
    this.activePath = window.sessionStorage.getItem('activePath')
  },
  methods: {
    // 保存当前激活的分类
    activeHandle (active) {
      window.sessionStorage.setItem('activePath', active)
      this.activePath = active
    },

    // 展开折叠分类
    toggleMenu() {
      this.isCollapse = !this.isCollapse
    },

    // 获取左侧菜单数据
    async getMenus() {
      // 发送请求
      const { data: res } = await this.$http.get('/menus')
      // 判断状态码
      if (res.meta.status !== 200) return this.$message.error(res.meta.msg)
      // 将取到的数据赋值给 data 中的属性
      this.menuList = res.data
    },

    // 退出方法
    logout() {
      // 清除本地的 token
      window.sessionStorage.clear()
      // 使用编程式导航跳转到登录页面
      this.$router.push('/login')
    }
  }
}
</script>

<style lang="less" scoped>
.home-container {
  height: 100%;

  .el-header {
    background-color: #373d41;
    display: flex;
    justify-content: space-between;
    padding-left: 0px;
    align-items: center;
    color: #fff;

    div {
      display: flex;
      align-items: center;
      font-size: 18px;

      span {
        margin-left: 16px;
      }
    }
  }

  .el-aside {
    background-color: #333744;

    .toggle-button {
      background-color: lightcoral;
      line-height: 24px;
      text-align: center;
      cursor: pointer;
      color: #fff;
      font-size: 14px;
      letter-spacing: 0.2em;
    }

    .el-menu {
      border: none;
    }

    .iconfont {
      margin-right: 10px !important;
    }
  }

  .el-main {
    background-color: #eaedf1;
  }
}
</style>
